package com.lovecar.view;


import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.lovecar.R;
import com.lovecar.server.MyJavaScript;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class WeiZhangActivity extends Activity {
	
	private EditText chepai;
	private EditText fadongji;
	private Button search;
	private TextView reslut;
	private Spinner haopaiType;
	String[] m_arr = {"01:大型汽车","02:小型汽车","06:外籍汽车","07:两、三轮摩托车","12:外籍摩托车","15:挂车","16:教练汽车"};
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.weizhang);		 
		 chepai=(EditText) findViewById(R.id.chepai);
		 fadongji=(EditText)findViewById(R.id.fadongji);
		 reslut=(TextView)findViewById(R.id.result);
		 search=(Button)findViewById(R.id.search);
		 haopaiType=(Spinner)findViewById(R.id.haopaiType);
		 
		 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_arr);
		 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
		 haopaiType.setAdapter(adapter);
		 
		 //int baseline = haopaiType.getBaseline();
		// haopaiType.performClick(); 
		 haopaiType.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
	          
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				//dispToast("选择的是"+m_arr[arg2]);
	            arg0.setVisibility(View.VISIBLE);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
	       });
		 //Toast.makeText(this, haopaiType.getPrompt() ,Toast.LENGTH_LONG).show();
		 search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String s=m_arr[haopaiType.getSelectedItemPosition()];
				System.out.println("---->"+s);
				s=s.split(":")[0];
				System.out.println("---->"+s);
				reslut.setText(test(s,chepai.getText().toString(),fadongji.getText().toString()));
			}
		});
		 //test("02","A431EV","8379");
	     
	     
	}
	public void dispToast(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }   
	/**
	 * 
	 * a:车牌种类
	 * b:车牌号
	 * c:发动机号
	 * **/
	public static String test(String a,String b,String c)
	{
		
		
		 String url="http://www.gzjd.gov.cn/gzwfcx/chaxunservlet?ywlx=cxlist";
	     String hpzl=a;//车牌种类
	     String hphm=b;//车牌号码
	     String fdjh=c;//发动机号
	     String jm="";//
	     String mode="1";
	     String hphm2="1";
	     String move="";
	     String sjc="粤";
	     
	     mode="2";
	     hphm2=sjc+hphm;
	     jm="156756dfgd75sdfsdf123fasdfsdfsdf"+hphm+fdjh;
	     hphm=hphm.toUpperCase();
	     
	     url="http://www.gzjd.gov.cn/gzwfcx/chaxunservlet?ywlx=cxlist&"
	    		 +"hpzl="+hpzl+"&"
	    		 +"hphm="+hphm+"&"
	    		 +"fdjh="+fdjh+"&"
	    		 +"jm="+jm+"&"
	    		 +"mode="+mode+"&"
	    		 +"hphm2="+hphm2+"&"
	    		 +"move="+move+"&"
	    		 +"sjc="+sjc+"&" 
	    		 +"sjc="+sjc+"&" 
	    		 ;
	     
	     String relult="";
	     try {
			Document doc = Jsoup.connect(url).get();
			Elements es=doc.getElementsByTag("table").get(1).getElementsByTag("tr");
			if(es.size()>1)
			{
				Elements e=es.get(1).getElementsByTag("td");
				relult="序号:"+e.get(0).text()+"\n"+
						"决定书编号:"+e.get(1).text()+"\n"+
						"违法时间:"+e.get(2).text()+"\n"+
						"违法地点:"+e.get(3).text()+"\n"+
						"违法来源:"+e.get(4).text()+"\n"+
						"处理状态:"+e.get(5).text()+"\n"					
						;
				System.out.println("::"+relult);			
			}
			else 
			{
				relult="没有违章记录";
			}
			System.out.println("::"+relult);						
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			relult="程序异常："+e.getMessage();
		}
	    return relult;
	}
}
